package day_02.sort;

public class QuickSort {
    public static void main(String[] args) {
        int[] arr = {10, 6, 5, 9, 8, 4, 3, 7, 1, 2};
        sort(arr);
        for (int value : arr) {
            System.out.print(value + " ");
        }
    }

    private static void sort(int[] arr){
        quickSort(arr, 0, arr.length - 1);
    }

    private static void quickSort(int[] arr, int l, int r) {
        if(l >= r) return;
        int p = partition(arr, l, r);
        quickSort(arr, l, p-1);
        quickSort(arr, p+1, r);
    }

    private static int partition(int[] arr, int l, int r) {
        int v = arr[l];
        int j = l;
        for (int i = l + 1; i <= r; i++) {
            if(arr[i] < v){
                swap(arr, j + 1, i);
                j++;
            }
        }
        swap(arr, j, l);
        return j;
    }

    private static void swap(int[] arr, int j, int i) {
        int tmp = arr[j];
        arr[j] = arr[i];
        arr[i] = tmp;
    }
}
